﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using EdmxLibrary;
using EdmxLibrary.DataObjects;
using EntityModel;


namespace TestDriver
{
	public class Program
	{
		static void Main()
		{
            List<Container> containers = GetCsdlByFilename();
            Debug.Assert(containers != null && containers.Count > 0);

            containers = GetCsdlByContext();
            Debug.Assert(containers != null && containers.Count > 0);

            Print(containers);

		    Console.WriteLine("\n\nPress Enter To Continue...");
			Console.ReadLine();
		}

        private static void Print(IEnumerable<Container> containers)
	    {
            foreach (Container container in containers)
            {
                Console.WriteLine(container.Name);
                Console.WriteLine("=====================================");

                container.Entities.ForEach(PrintEntity);
                container.Structures.ForEach(PrintEntity);
            }
	    }

	    private static void PrintEntity(Entity entity)
	    {
            Console.WriteLine("\n\n");
            Console.WriteLine(entity.Name);
            Console.WriteLine("-------------------------------------");

            entity.Keys.ForEach(PrintScalar);
            Console.WriteLine("");
            entity.Scalars.ForEach(PrintScalar);
            Console.WriteLine("");
            entity.SingleReferences.ForEach(PrintReference);
            Console.WriteLine("");
            entity.MultipleReferences.ForEach(PrintReference);
	    }

        private static void PrintScalar(Scalar scalar)
        {
            Console.WriteLine(string.Format("{0,25} {1,-25} | {2,5} | {3,3}{4,1}{5,-2} |", 
                scalar.ScalarType.Name, scalar.Name, 
                (scalar.MaxLength.HasValue) ? scalar.MaxLength.Value.ToString() : string.Empty,
                (scalar.Precision.HasValue) ? scalar.Precision.Value.ToString() : string.Empty,
                (scalar.Scale.HasValue) ? "." : string.Empty,
                (scalar.Scale.HasValue) ? scalar.Scale.Value.ToString() : string.Empty));
        }

        private static void PrintReference(Reference reference)
        {
            Console.WriteLine(string.Format("{0,25} {1,-25}", reference.ReferenceType.Name, reference.Name));
        }

	    private static List<Container> GetCsdlByContext()
        {
            using (AdventureWorksEntities context = new AdventureWorksEntities())
            {
                return EdmxHelper.Parse(context);
            }
        }

        private static List<Container> GetCsdlByFilename()
        {
            return EdmxHelper.Parse(GetFilename());
        }

        private static string GetFilename()
        {
            const string edmxFilename = @"..\EntityModel\AdventureWorks.edmx";
            const string baseDir = @"..\..";

            return Path.Combine(baseDir, edmxFilename);
        }
	}
}
